package com.cjxy.parking.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjxy.parking.common.R;
import com.cjxy.parking.entity.po.Notice;
import com.cjxy.parking.entity.query.NoticeQuery;
import com.cjxy.parking.entity.vo.MsgVo;
import com.cjxy.parking.service.NoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @auther huangsenji
 * @date 2025/10/26
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeService noticeService;

    /**
     * 分页查询公告列表
     *
     * @param noticeQuery 公告查询条件
     * @return 公告列表
     */
    @PostMapping("/pageNotice")
    public R<IPage<Notice>> pageNotice(@RequestBody NoticeQuery noticeQuery) {
        if (Objects.isNull(noticeQuery)) {
            throw new RuntimeException("参数错误");
        }
        return R.success(noticeService.pageNotice(noticeQuery));
    }

    /**
     * 添加公告
     *
     * @param notice 公告信息
     * @return 添加结果
     */
    @PostMapping("/save")
    public R<MsgVo> addNotice(@RequestBody Notice notice) {
        if (Objects.isNull(notice) || StrUtil.isBlank(notice.getTitle()) || StrUtil.isBlank(notice.getContent())) {
            throw new RuntimeException("参数错误，请重新输入");
        }
        boolean flag = noticeService.saveNotice(notice);
        if (flag) {
            return R.success(new MsgVo(true, "添加公告成功"));
        } else {
            return R.success(new MsgVo(false, "添加公告失败"));
        }
    }

    /**
     * 更新公告
     *
     * @param notice 公告信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public R<MsgVo> updateNotice(@RequestBody Notice notice) {
        notice.setUpdateTime(new Date());
        boolean flag = noticeService.updateNotice(notice);
        if (flag) {
            return R.success(new MsgVo(true, "更新公告成功"));
        } else {
            return R.success(new MsgVo(false, "更新公告失败"));
        }
    }

    /**
     * 删除公告（逻辑删除）
     *
     * @param id 公告ID
     * @return 删除结果
     */
    @GetMapping("/del")
    public R<MsgVo> deleteNotice(Long id) {
        if (id == null) {
            throw new RuntimeException("参数错误");
        }
        Notice notice = noticeService.getById(id);
        if (Objects.isNull(notice)) {
            throw new RuntimeException("公告不存在");
        }
        notice.setStatus(0);
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return R.success(new MsgVo(true, "删除公告成功"));
        } else {
            return R.success(new MsgVo(false, "删除公告失败"));
        }
    }


    /**
     *  获取最新的公告
     */
    @GetMapping("/getLast")
    public R<Notice> getLastNotice() {
        Notice notice = noticeService.getLastNotice();
        return R.success(notice);
    }

    /**
     * 获取最新三条
     */
    @GetMapping("/getLastThree")
    public R<List<Notice>> getLastThree() {
        List<Notice> notice = noticeService.getLastThree();
        return R.success(notice);
    }

    /**
     * 根据ID获取公告详情
     *
     * @param id 公告ID
     * @return 公告详情
     */
    @GetMapping("/get")
    public R<Notice> getNotice(Long id) {
        Notice notice = noticeService.getById(id);
        return R.success(notice);
    }
}